<?php
/**
 * 自定义一个服务端入口 相当于一个web服务器
 * php http_server.php
 *
 * 客户端调用
 * curl "http://127.0.0.1:9501/?a=b"
 * curl http://127.0.0.1:9501/\?a=b
 *
 * 请求十次
 * echo {1..10} | xargs -n1 -I{} curl "http://127.0.0.1:9501/?a=b"
 */
$http = new swoole_http_server("127.0.0.1", 9501);

/**
 * 服务端启动的时候, 被调用
 */
$http->on("start", function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

/**
 * 客户端连接的时候 被调用
 */
$http->on('connect', function ($server, $fd) {
    echo "Client:Connect:{$fd}\n";
});

/**
 * 客户端关闭连接的时候 被调用
 */
$http->on('close', function ($server, $fd) {
    echo "connection close: {$fd}\n";
});


/**
 * 接收到http请求时 被调用
 */
$http->on("request", function ($request, $response) {
    //路由处理
    if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
        return $response->end();
    }
    $data = [
        'get' => $request->get,
        'post' => $request->post,
    ];
    var_dump($data);
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>");
});

$http->start();